<template>
    <form id="data-form-container" @submit.prevent="handleSubmit" class="data-form-container" ref="form">
        <div class="form-item">
            <div class="input-area">
                <input type="text" maxlength="10" placeholder="用户昵称" v-model="formatData.nickname" />
                <span class="tip">{{ formatData.nickname.length }}/10</span>
            </div>
            <div class="error">{{ error.nickname }}</div>
        </div>
        <div class="form-item">
            <div class="text-area">
                <textarea placeholder="输入内容" maxlength="300" v-model="formatData.content"></textarea>
                <span class="tip">{{ formatData.content.length }}/300</span>
            </div>
            <div class="error">{{ error.content }}</div>
        </div>
        <div class="form-item">
            <div class="button-area">
                <button :disabled="isSubmiting">{{ isSubmiting ? "提交中..." : "提交" }}</button>
            </div>
        </div>
    </form>
</template>

<script>
export default {
    data() {
        return {
            formatData: {
                nickname: "",
                content: "",
            },
            error: {
                nickname: "",
                content: ""
            },
            isSubmiting: false,
        }
    },
    methods: {
        handleSubmit() {
            this.error.nickname = this.formatData.nickname ? "" : "请填写昵称"
            this.error.content = this.formatData.content ? "" : "请填写内容"
            if (this.error.nickname || this.error.content) {
                // 有错误
                return;
            }
            this.isSubmiting = true;  //正在提交防止重复点击
            this.$emit("submit", this.formatData, (successMsg) => {
                this.$showMessage({
                    content: successMsg,
                    type: "success",
                    duration: 1000,
                    container:this.$refs.form,
                    callback: () => {
                        this.isSubmiting = false;
                        this.formatData.nickname = "";
                        this.formatData.content = "";
                    }
                })
            });  //让父组件处理事件
        }
    }
}
</script>

<style scoped lang="less">
@import "~@/styles/var.less";

.data-form-container {
    margin-bottom: 20px;
    overflow: hidden;
}

.form-item {
    margin-bottom: 8px;
}

.input-area {
    width: 50%;
    position: relative;
}

.text-area {
    position: relative;
}

input,
textarea {
    display: flex;
    width: 100%;
    box-sizing: border-box;
    border: 1px solid @gray;
    outline: none;
    color: @words;
    font-size: 14px;
    border-radius: 4px;

    &:focus {
        border-color: @primary;
    }
}

input {
    padding: 0 15px;
    height: 40px;
}

textarea {
    resize: none;
    padding: 8px 15px;
    height: 120px;
}

.error {
    margin-top: 6px;
    color: @danger;
    font-size: 14px;
    height: 20px;
    line-height: 20px;
}

button {
    position: relative;
    cursor: pointer;
    border: none;
    outline: none;
    width: 100px;
    color: #fff;
    border-radius: 4px;
    background: @primary;

    &:hover {
        background: darken(@primary, 10%);
    }

    &:disabled {
        background: lighten(@primary, 20%);
        cursor: not-allowed;
    }
}
</style>